package com.mall.server.controller.sms;

import com.mall.model.domain.SmsFlashPromotion;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.server.service.sms.SmsFlashPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/flash")
public class SmsFlashPromotionController {

    @Autowired
    private SmsFlashPromotionService smsFlashPromotionService;


    /**
     * 根据活动名称分页查询
     */
    @GetMapping("/list")
    public CommonResult list(String keyword
            , @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "10") Integer pageSize) {

        CommonPage cp = smsFlashPromotionService.list(keyword, pageNum, pageSize);

        return CommonResult.success(cp);
    }

    /**
     * 添加活动
     */
    @PostMapping("/create")
    public CommonResult saveFlash(@RequestBody SmsFlashPromotion flashPromotion) {

        smsFlashPromotionService.saveFlash(flashPromotion);

        return CommonResult.success(null);
    }

    /**
     * 删除活动
     */
    @PostMapping("/delete/{id}")
    public CommonResult deleteFlash(@PathVariable("id") Integer id) {

        smsFlashPromotionService.deleteFlash(id);

        return CommonResult.success(null);
    }

    /**
     * 修改上下线状态
     */
    @PostMapping("/update/status/{id}")
    public CommonResult updateStatus(@PathVariable("id") Integer id, SmsFlashPromotion smsFlashPromotion) {

        smsFlashPromotionService.updateFlash(id, smsFlashPromotion);

        return CommonResult.success(null);
    }

    /**
     * 编辑活动
     */
    @PostMapping("/update/{id}")
    public CommonResult updateFlash(@PathVariable("id") Integer id, @RequestBody SmsFlashPromotion smsFlashPromotion) {

        smsFlashPromotionService.updateFlash(id, smsFlashPromotion);

        return CommonResult.success(null);
    }

    /**
     * 获取活动详情
     */
    @GetMapping("/{id}")
    public CommonResult findById(@PathVariable("id") Integer id) {

        SmsFlashPromotion flashPromotion = smsFlashPromotionService.findById(id);

        return CommonResult.success(flashPromotion);
    }


}
